#include <layout.h>
#include <mmu.h>
		
#include "boot_ap.S.h"
		
		.set CR0_PE_ON,      0x1    
		.set CR0_PG_ON,      0x80000000

		.align PAGE_SIZE
		.globl boot_ap_entry
boot_ap_entry:
		.code16

		cli
		cld

		xorw    %ax,%ax             # Segment number zero
		movw    %ax,%ds             # -> Data Segment
		movw    %ax,%es             # -> Extra Segment
		movw  	%ax,%ss             # -> Stack Segment

		movl	$BOOT_AP_GDT_DESC, %eax
#		lgdt    (%eax)
		movl  	%cr0, %eax
		orl     $CR0_PE_ON, %eax
		movl    %eax, %cr0

		ljmp    $GD_KERN_TEXT, $(boot_ap_relocated - boot_ap_entry + BOOT_AP_ENTRY)
		.code32		
boot_ap_relocated:
		movw    $GD_KERN_DATA, %ax    
		movw  	%ax, %ds                
		movw    %ax, %es               
		movw    %ax, %fs               
		movw    %ax, %gs              
		movw    %ax, %ss

		movl    $(boot_ap_vpd - KBASE), %eax
		movl    %eax, %cr3
		movl  	%cr0, %eax
		orl     $CR0_PG_ON, %eax
		movl    %eax, %cr0

		movl  	$0x0, %ebp
	    movl  	$(boot_ap_stack_top), %esp
		
		movl  	$boot_ap_c_entry, %eax
		call 	*%eax
loop:
		jmp loop
		
		.data
		
		.align PAGE_SIZE
boot_ap_stack:
	    .space KSTACK_SIZE
	    .globl init_stack_top
boot_ap_stack_top:
		
		.align PAGE_SIZE
		.globl boot_ap_gdt
boot_ap_gdt:
		SEG_NULL
	    SEG(STA_X | STA_R, 0, 0xffffffff)
	    SEG(STA_W, 0, 0xffffffff)
		
		.globl boot_ap_gdt_desc
		.align PAGE_SIZE
boot_ap_gdt_desc:	
		.word   0x17
		.long   BOOT_AP_GDT
